<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Geo Functions | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'sql-functions-geo.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/sql-functions-geo.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/sql-functions-geo.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/sql-functions-geo.html" rel="nofollow" target="_blank">../en/sql-functions-geo.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="xpack-sql.html">SQL access</a></span>
»
<span class="breadcrumb-link"><a href="sql-functions.html">Functions and Operators</a></span>
»
<span class="breadcrumb-node">Geo Functions</span>
</div>
<div class="navheader">
<span class="prev">
<a href="sql-functions-type-conversion.html">« Type Conversion Functions</a>
</span>
<span class="next">
<a href="sql-functions-conditional.html">Conditional Functions And Expressions »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="sql-functions-geo"></a>Geo Functions<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/geo.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>
<div class="warning admon">
<div class="icon"></div>
<div class="admon_content">
<p>This functionality is in beta and is subject to change. The design and code is less mature than official GA features and is being provided as-is with no warranties. Beta features are not subject to the support SLA of official GA features.</p>
</div>
</div>
<p>The geo functions work with geometries stored in <code class="literal">geo_point</code>, <code class="literal">geo_shape</code> and <code class="literal">shape</code> fields, or returned by other geo functions.</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_limitations"></a>Limitations<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/geo.asciidoc">edit</a>
</h3>
</div></div></div>
<p><a class="xref" href="geo-point.html" title="Geo-point datatype"><code class="literal">geo_point</code></a>, <a class="xref" href="geo-shape.html" title="Geo-shape datatype"><code class="literal">geo_shape</code></a> and <a class="xref" href="shape.html" title="Shape datatype"><code class="literal">shape</code></a> and types are represented in SQL as
geometry and can be used interchangeably with the following exceptions:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<code class="literal">geo_shape</code> and <code class="literal">shape</code> fields don’t have doc values, therefore these fields cannot be used for filtering, grouping
or sorting.
</li>
<li class="listitem">
<code class="literal">geo_points</code> fields are indexed and have doc values by default, however only latitude and longitude are stored and
indexed with some loss of precision from the original values (4.190951585769653E-8 for the latitude and
8.381903171539307E-8 for longitude). The altitude component is accepted but not stored in doc values nor indexed.
Therefore calling <code class="literal">ST_Z</code> function in the filtering, grouping or sorting will return <code class="literal">null</code>.
</li>
</ul>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_geometry_conversion"></a>Geometry Conversion<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/geo.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="sql-functions-geo-st-as-wkt"></a><code class="literal">ST_AsWKT</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/geo.asciidoc">edit</a>
</h4>
</div></div></div>
<p><strong>Synopsis:</strong></p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">ST_AsWKT(
    geometry <a id="CO164-1"></a><i class="conum" data-value="1"></i>
)</pre>
</div>
<p><span class="strong strong"><strong>Input</strong></span>:</p>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO164-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>geometry</p>
</td>
</tr>
</table>
</div>
<p><span class="strong strong"><strong>Output</strong></span>: string</p>
<p><span class="strong strong"><strong>Description</strong></span>: Returns the WKT representation of the <code class="literal">geometry</code>.</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT city, ST_AsWKT(location) location FROM "geo" WHERE city = 'Amsterdam';

     city:s    |     location:s
Amsterdam      |POINT (4.850311987102032 52.347556999884546)</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="sql-functions-geo-st-wkt-to-sql"></a><code class="literal">ST_WKTToSQL</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/geo.asciidoc">edit</a>
</h4>
</div></div></div>
<p><strong>Synopsis:</strong></p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">ST_WKTToSQL(
    string <a id="CO165-1"></a><i class="conum" data-value="1"></i>
)</pre>
</div>
<p><span class="strong strong"><strong>Input</strong></span>:</p>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO165-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>string WKT representation of geometry</p>
</td>
</tr>
</table>
</div>
<p><span class="strong strong"><strong>Output</strong></span>: geometry</p>
<p><span class="strong strong"><strong>Description</strong></span>: Returns the geometry from WKT representation.</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT CAST(ST_WKTToSQL('POINT (10 20)') AS STRING) location;

   location:s
POINT (10.0 20.0)</pre>
</div>
</div>

</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_geometry_properties"></a>Geometry Properties<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/geo.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="sql-functions-geo-st-geometrytype"></a><code class="literal">ST_GeometryType</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/geo.asciidoc">edit</a>
</h4>
</div></div></div>
<p><strong>Synopsis:</strong></p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">ST_GeometryType(
    geometry <a id="CO166-1"></a><i class="conum" data-value="1"></i>
)</pre>
</div>
<p><span class="strong strong"><strong>Input</strong></span>:</p>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO166-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>geometry</p>
</td>
</tr>
</table>
</div>
<p><span class="strong strong"><strong>Output</strong></span>: string</p>
<p><span class="strong strong"><strong>Description</strong></span>: Returns the type of the <code class="literal">geometry</code> such as POINT, MULTIPOINT, LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON, GEOMETRYCOLLECTION, ENVELOPE or CIRCLE.</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT ST_GeometryType(ST_WKTToSQL('POINT (10 20)')) type;

      type:s
POINT</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="sql-functions-geo-st-x"></a><code class="literal">ST_X</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/geo.asciidoc">edit</a>
</h4>
</div></div></div>
<p><strong>Synopsis:</strong></p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">ST_X(
    geometry <a id="CO167-1"></a><i class="conum" data-value="1"></i>
)</pre>
</div>
<p><span class="strong strong"><strong>Input</strong></span>:</p>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO167-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>geometry</p>
</td>
</tr>
</table>
</div>
<p><span class="strong strong"><strong>Output</strong></span>: double</p>
<p><span class="strong strong"><strong>Description</strong></span>: Returns the longitude of the first point in the geometry.</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT ST_X(ST_WKTToSQL('POINT (10 20)')) x;

      x:d
10.0</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="sql-functions-geo-st-y"></a><code class="literal">ST_Y</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/geo.asciidoc">edit</a>
</h4>
</div></div></div>
<p><strong>Synopsis:</strong></p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">ST_Y(
    geometry <a id="CO168-1"></a><i class="conum" data-value="1"></i>
)</pre>
</div>
<p><span class="strong strong"><strong>Input</strong></span>:</p>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO168-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>geometry</p>
</td>
</tr>
</table>
</div>
<p><span class="strong strong"><strong>Output</strong></span>: double</p>
<p><span class="strong strong"><strong>Description</strong></span>: Returns the latitude of the first point in the geometry.</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT ST_Y(ST_WKTToSQL('POINT (10 20)')) y;

      y:d
20.0</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="sql-functions-geo-st-z"></a><code class="literal">ST_Z</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/geo.asciidoc">edit</a>
</h4>
</div></div></div>
<p><strong>Synopsis:</strong></p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">ST_Z(
    geometry <a id="CO169-1"></a><i class="conum" data-value="1"></i>
)</pre>
</div>
<p><span class="strong strong"><strong>Input</strong></span>:</p>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO169-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>geometry</p>
</td>
</tr>
</table>
</div>
<p><span class="strong strong"><strong>Output</strong></span>: double</p>
<p><span class="strong strong"><strong>Description</strong></span>: Returns the altitude of the first point in the geometry.</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT ST_Z(ST_WKTToSQL('POINT (10 20 30)')) z;

      z:d
30.0</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="sql-functions-geo-st-distance"></a><code class="literal">ST_Distance</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/geo.asciidoc">edit</a>
</h4>
</div></div></div>
<p><strong>Synopsis:</strong></p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">ST_Distance(
    geometry, <a id="CO170-1"></a><i class="conum" data-value="1"></i>
    geometry  <a id="CO170-2"></a><i class="conum" data-value="2"></i>
)</pre>
</div>
<p><span class="strong strong"><strong>Input</strong></span>:</p>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO170-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>source geometry</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO170-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>target geometry</p>
</td>
</tr>
</table>
</div>
<p><span class="strong strong"><strong>Output</strong></span>: Double</p>
<p><span class="strong strong"><strong>Description</strong></span>: Returns the distance between geometries in meters. Both geometries have to be points.</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT ST_Distance(ST_WKTToSQL('POINT (10 20)'), ST_WKTToSQL('POINT (20 30)')) distance;

   distance:d
1499101.2889383635</pre>
</div>
</div>

</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="sql-functions-type-conversion.html">« Type Conversion Functions</a>
</span>
<span class="next">
<a href="sql-functions-conditional.html">Conditional Functions And Expressions »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>